package com.java110.mall.goods.bmo.mainCategoryProduct.impl;

import com.java110.core.annotation.Java110Transactional;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.housekeeping.HousekeepingServDto;
import com.java110.dto.mainCategory.MainCategoryDto;
import com.java110.dto.mainCategory.MainCategoryProductDto;
import com.java110.dto.product.ProductDto;
import com.java110.mall.goods.bmo.mainCategoryProduct.ISaveMainCategoryProductBMO;
import com.java110.intf.goods.IHousekeepingServInnerServiceSMO;
import com.java110.intf.goods.IMainCategoryInnerServiceSMO;
import com.java110.intf.goods.IMainCategoryProductInnerServiceSMO;
import com.java110.intf.goods.IProductInnerServiceSMO;
import com.java110.po.mainCategory.MainCategoryProductPo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("saveMainCategoryProductBMOImpl")
public class SaveMainCategoryProductBMOImpl implements ISaveMainCategoryProductBMO {

    @Autowired
    private IMainCategoryProductInnerServiceSMO mainCategoryProductInnerServiceSMOImpl;

    @Autowired
    private IProductInnerServiceSMO productInnerServiceSMOImpl;

    @Autowired
    private IHousekeepingServInnerServiceSMO housekeepingServInnerServiceSMO;

    @Autowired
    private IMainCategoryInnerServiceSMO mainCategoryInnerServiceSMO;


    /**
     * 添加小区信息
     *
     * @param mainCategoryProductPo
     * @return 订单服务能够接受的报文
     */
    @Java110Transactional
    public ResponseEntity<String> save(MainCategoryProductPo mainCategoryProductPo) {
        //校验商品、服务是否存在
        MainCategoryDto mainCategoryDto = new MainCategoryDto();
        mainCategoryDto.setMainCategoryId(mainCategoryProductPo.getMainCategoryId());
        List<MainCategoryDto> queryMainCategorys = mainCategoryInnerServiceSMO.queryMainCategorys(mainCategoryDto);
        Assert.listOnlyOne(queryMainCategorys, "专区不存在");

        String ctype = queryMainCategorys.get(0).getCategoryType();
        String shopid = "";
        String pname = "";
        //1,家政;2,商圈

        HousekeepingServDto housekeepingServDto = new HousekeepingServDto();
        housekeepingServDto.setServId(mainCategoryProductPo.getProductId());
        List<HousekeepingServDto> housekeepingServDtos = housekeepingServInnerServiceSMO.queryHousekeepingServs(housekeepingServDto);

        if (!ListUtil.isNull(housekeepingServDtos)) {
            shopid = housekeepingServDtos.get(0).getShopId();
            pname = housekeepingServDtos.get(0).getServName();
        }

        ProductDto productDto = new ProductDto();
        productDto.setProductId(mainCategoryProductPo.getProductId());
        List<ProductDto> productDtos = productInnerServiceSMOImpl.queryProducts(productDto);
        if (!ListUtil.isNull(productDtos)) {
            shopid = productDtos.get(0).getShopId();
            pname = productDtos.get(0).getProdName();
        }

        if (StringUtil.isEmpty(shopid)) {
            throw new IllegalArgumentException("家政服务或者商品不存在");
        }


        mainCategoryProductPo.setMcProductId(GenerateCodeFactory.getGeneratorId("13"));
        mainCategoryProductPo.setState(MainCategoryProductDto.STATE_W);
        mainCategoryProductPo.setMessage("待审核");
        mainCategoryProductPo.setShopId(shopid);
        mainCategoryProductPo.setProductName(pname);
        int flag = mainCategoryProductInnerServiceSMOImpl.saveMainCategoryProduct(mainCategoryProductPo);

        if (flag > 0) {
            return ResultVo.createResponseEntity(ResultVo.CODE_OK, "保存成功");
        }

        return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "保存失败");
    }

}
